<?php
/*
 * Copyright (c) 2021.  Liuhongbin All rights reserved.
 * @domain http://dkyx.cc
 * @author liuhongbin <514660094@qq.com>
 */

namespace app\controller\adminapi\platform;

use app\common\model\addon\Addons;
use app\common\model\platform\PlatformAppUser as PlatformAppUserModel;
use app\common\model\user\User;
use hiduoke\services\FormBuilder as Form;
use think\App;

/**
 * 平台应用小程序的用户次数
 */
class PlatformAppUser extends PlatformController
{
    /**
     * @var PlatformAppUserModel
     */
    private PlatformAppUserModel $model;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new PlatformAppUserModel();
    }

    public function getPlatformAppUserList()
    {
        $res = $this->model->getPageList();
        return $this->success($res['count'], $res['data']);
    }

    public function getPlatformAppUserForm($id)
    {
        $rule = array();
        //创建表单
        $form = Form::createForm("/adminapi/platform/app_user/AppUserFormSave?id=" . $id)->setMethod("POST");
        $row = $this->model->where('id', $id)->findOrEmpty();
        //新增表单
        $rule[] = Form::hidden('addon_name', input('addon_name'));
        if ($row->isEmpty()) {
            $rule[] = Form::radio("type", "查询方式", 0)
                ->options([['value' => 0, 'label' => '昵称'], ['value' => 1, 'label' => '用户ID']])
                ->appendControl(0, [Form::dkInput('username', '用户昵称', "", "填写用户昵称")->required()])
                ->appendControl(1, [
                    Form::dkInputNumber("uid", "用户UID", "", "填写用户的UID")->required(),
                ]);
        }
        $rule[] = Form::dkInputNumber('count', '小程序次数', $row['count'] ?? 1, '设置当前用户可以创建该小程序的次数');
        $form->setTitle("新增用户应用次数")->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    public function AppUserFormSave()
    {
        $formData = getFormData();
        $id = input('id');
        $res = $this->model->where(['id' => $id])->findOrEmpty();
        if ($res->isEmpty()) {
            // 判断是否存在
            if ($formData['type'] == 1) {
                $user = (new User())->where(['uid' => $formData['uid']])->find();
                if (!$user) {
                    return $this->error("用户UID有误，当前用户不存在");
                }
            } else {
                $user = (new User())->where(['username' => $formData['username']])->find();
                if (!$user) {
                    return $this->error("用户昵称有误，当前用户不存在");
                }
            }
            $addon = (new Addons())->where('name', $formData['addon_name'])->find();
            if (!$addon) {
                return $this->error("当前应用不存在或者已停用");
            }
            $app_user_count = (new PlatformAppUserModel())->where(['uid' => $user['uid'], 'addon_name' => $addon['name']])->count();
            // 判断是否已经存在
            if ($app_user_count) {
                return $this->error('当前用户已经存在该应用');
            }
            $data = array(
                'uid'        => $user['uid'],
                'addon_name' => $addon['name'],
                'app_type'   => $addon['app_type'],
                'count'      => $formData['count'],
            );
            $res->save($data);
            return $this->success("新增成功");
        } else {
            $res->save([
                'count' => $formData['count']
            ]);
            return $this->success('修改成功');
        }
    }
}
